import nedb from './nedb'
export default {
  /**
   * 获取客服的自定义回复
   * @param {int} csId 客服ID
   */
  async getCustomReply (csId) {
    try {
      var result = await nedb.custom_reply.find({
        cs_id: csId
      })
      if (result.length > 0) {
        return Promise.resolve(result[0].data)
      }
      return Promise.resolve([])
    } catch (e) {
      return Promise.resolve([])
    }
  },
  /**
   * @param {int} csId 客服ID
   * @param {Array} data 自定义回复内容
   */
  async addCustomReply (csId, data) {
    try {
      var oldResult = await nedb.custom_reply.find({
        cs_id: csId
      })
      if (oldResult.length > 0) {
        await nedb.custom_reply.update({cs_id: csId}, {
          cs_id: csId,
          data: data
        })
      } else {
        await nedb.custom_reply.insert({
          cs_id: csId,
          data: data
        })
      }
      return Promise.resolve(data)
    } catch (e) {
      return Promise.resolve([])
    }
  }
}
